<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */

class FileController extends HomeController {
	protected function _initialize(){
        require_once('ThinkPHP/Library/Vendor/api_payment/llpaypayment.php');
        require_once('ThinkPHP/Library/Vendor/OCR_API.php');


        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        $options = array(
            'token'     => $this->token,   // 填写你设定的key
            'appid'     => $this->appid,    // 填写高级调用功能的appid
            'appsecret' => $this->appsecret // 填写高级调用功能的密钥
        );
        $this->weixin = new \Common\Extend\Wechat\Wechat($options);
        $weixin = cookie('WeixinLogin');
        if(empty($weixin)){
            $this->oauth();
        }else{
            $this->uid          = $weixin['uid'];
            $this->access_token = $weixin['access_token'];
            $this->openid       = $weixin['openid'];
        }
        $where['id'] =$this->uid;
        $count = D('ucenter_member')->where($where)->getField('mobile');
        if(empty($count)){
            $this->redirect('User/mobile');
        }
        
    }
    
	/* 文件上传 */
	public function upload(){
		$return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
		/* 调用文件上传组件上传文件 */
		$File = D('File');
		$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
		$info = $File->upload(
			$_FILES,
			C('DOWNLOAD_UPLOAD'),
			C('DOWNLOAD_UPLOAD_DRIVER'),
			C("UPLOAD_{$file_driver}_CONFIG")
		);

		/* 记录附件信息 */
		if($info){
			$return['data'] = think_encrypt(json_encode($info['download']));
		} else {
			$return['status'] = 0;
			$return['info']   = $File->getError();
		}

		/* 返回JSON数据 */
		$this->ajaxReturn($return);
	}

	/* 下载文件 */
	public function download($id = null){
		if(empty($id) || !is_numeric($id)){
			$this->error('参数错误！');
		}

		$logic = D('Download', 'Logic');
		if(!$logic->download($id)){
			$this->error($logic->getError());
		}
		
	}
}
